//
//  ClassifyViewController.m
//  DaysComicSugar
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ClassifyViewController.h"
#import "ComicClassifyCell.h"
#import "MJRefresh.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "ClassifyModel.h"
#import "ClassifyDetailsViewController.h"

@interface ClassifyViewController ()
@property(nonatomic, retain)UICollectionView *classifyCV;
@property(nonatomic, retain)NSMutableArray *classifyArr;//存放所有字典的数组
@end

@implementation ClassifyViewController

#pragma mark 漫画分类初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"漫画分类";
        self.classifyArr = [NSMutableArray array];
    }
    return self;
}
#pragma mark 漫画分类内存释放
-(void)dealloc
{
    [_classifyCV release];
    [_classifyArr release];
    [super dealloc];
}

#pragma mark 漫画分类viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestData];
    [self createView];
    [self addHeader];
    [self addFooter];
}

#pragma mark 创建视图
- (void)createView
{
    //创建一个瀑布流
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 160);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    //创建一个collectionView
    self.classifyCV = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    self.classifyCV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.classifyCV];
    [_classifyCV release];
    self.classifyCV.dataSource = self;
    self.classifyCV.delegate = self;
    //注册cell
    [self.classifyCV registerClass:[ComicClassifyCell class] forCellWithReuseIdentifier:@"reuse"];
}

#pragma mark 协议里面有两个必须实现的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.classifyArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ComicClassifyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    ClassifyModel *classify = self.classifyArr[indexPath.row];
    cell.classifyLabel.text = classify.CatalogName;
    [cell.classifyImageView sd_setImageWithURL:[NSURL URLWithString:classify.CatalogOtherURL]];
    return cell;
}

#pragma mark 解析数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=catalogs" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSMutableArray *array = responseObject;
        NSLog(@"加载成功");
        for (int i = 0; i < array.count; i++) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setValuesForKeysWithDictionary:array[i]];
            ClassifyModel *classify = [[ClassifyModel alloc] init];
            [classify setValuesForKeysWithDictionary:dic];
            [self.classifyArr addObject:classify];
            [self.classifyCV reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
        NSLog(@"%@", error);
    }];
}

#pragma mark collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyDetailsViewController *classifyDetailsVC = [[ClassifyDetailsViewController alloc] init];
    [self.navigationController pushViewController:classifyDetailsVC animated:YES];
    [classifyDetailsVC release];
}

#pragma mark 上拉刷新
- (void)addHeader
{
    NSLog(@"刷新");
//    [self.classifyCV addHeaderWithCallback:^{
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self.classifyCV reloadData];
//            [self.classifyCV headerEndRefreshing];
//        });
//    }];
}

#pragma mark 下拉加载
- (void)addFooter
{
    NSLog(@"加载");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
